import java.net.URI;
import java.util.ArrayList;

import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.UriBuilder;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.JSONValue;

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.config.ClientConfig;
import com.sun.jersey.api.client.config.DefaultClientConfig;



public class ClienLourd {

	/**
	 * @param args
	 */
	private static URI getBaseURI()
    {
    	return UriBuilder.fromUri("https://api.twitter.com/1/statuses/user_timeline.json?screen_name=remixjobs").build();
	}
	
	public static ArrayList<Offre> JSONToOffre(String json)
	{
		  ArrayList<Offre> listeOffre = new ArrayList<Offre>();
		  
		  System.out.println("=======decode=======");
		  Object obj=JSONValue.parse(json);
		  JSONArray array=(JSONArray)obj;
		  
		  for (int i=1;i<array.size();i++)
		  {
			  Offre of = new Offre();
			  JSONObject user = (JSONObject) ((JSONObject)array.get(i)).get("user");
			  String desc = (String) user.get("description");
			  JSONObject place = (JSONObject) ((JSONObject)array.get(i)).get("place");
			  if(place != null)
			  {
				  String ville = (String) place.get("name");
				  of.lieu = ville;
			  }
			  of.description = desc;
			  String s = (String) ((JSONObject)array.get(i)).get("text");
			  of.id = (long) i;
			  String[] liste;
			  String[] sousListe;
			  if(s.contains("["))
			  {
				  liste = s.split("-");
				  sousListe = liste[0].split("]");
				  of.type = sousListe[0].substring(1);
				  of.intitule = sousListe[1];
				  listeOffre.add(of);
			  }
		  } 
		  

		return listeOffre;
	}
	
	
	public static void main(String[] args) 
	{
	    ClientConfig config = new DefaultClientConfig();
	    Client client = Client.create(config);
	    WebResource service = client.resource(getBaseURI());
	    String reponse = service.accept(MediaType.TEXT_XML).get(String.class);
	    System.out.println(reponse);
	    ArrayList<Offre> liste = ClienLourd.JSONToOffre(reponse);
	    
	} 
	
}